﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.VehicleParts.Elements;

namespace JBeamLibrary.VehicleParts.Groups
{
    public class Ropables : DictionaryBase
    {
        #region Indexers

        /// <summary>Gets the ropable at the specified index</summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Ropable this[int index]
        {
            get
            {
                if (Count == 0)
                    return null;
                if (index < 0 || index >= Count)
                    return null;
                return Values.ElementAt(index);
            }
        }

        /// <summary>Gets the ropable element for the given node</summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public Ropable this[string nodeId]
        {
            get { return (Ropable)Dictionary[nodeId]; }
        }

        #endregion

        #region Properties

        /// <summary>Gets the ropables in a value collection</summary>
        public IEnumerable<Ropable> Values
        {
            get { return Dictionary.Values.Cast<Ropable>(); }
        }

        #endregion

        #region CTORs

        public Ropables()
        {

        }

        public Ropables(IEnumerable<Ropable> ropables)
        {
            AddRange(ropables);
        }

        #endregion

        #region Methods

        public bool Add(string nodeId, string group, bool multiLock)
        {
            return Add(new Ropable(nodeId, group, multiLock));
        }

        public bool Add(Ropable ropable)
        {
            if (Dictionary.Contains(ropable.NodeId)) return false;
            Dictionary.Add(ropable.NodeId, ropable);
            return true;
        }

        public void AddRange(IEnumerable<Ropable> ropables)
        {
            foreach (var ropable in ropables)
                Add(ropable);
        }

        public void UpdateAdd(string nodeId, string group, bool multiLock)
        {
            UpdateAdd(new Ropable(nodeId, group, multiLock));
        }

        public void UpdateAdd(Ropable ropable)
        {
            if (Dictionary.Contains(ropable.NodeId))
                Dictionary[ropable.NodeId] = ropable;
            else
                Add(ropable);
        }

        public void UpdateAddRange(IEnumerable<Ropable> ropables)
        {
            foreach (var ropable in ropables)
                UpdateAdd(ropable);
        }

        public void Remove(string nodeId)
        {
            Dictionary.Remove(nodeId);
        }

        #endregion
    }
}
